<?php
declare (strict_types = 1);

namespace app\index\model;

use think\Exception;
use think\facade\Config;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class Brand extends Model
{
    protected $pk="brand_id";
    use SoftDelete;
    protected $deleteTime = 'del';
    protected $error;

    public function getError(){
        return $this->error;
    }
    public function searchBrandNameAttr($query,$value,$data){
        if (""!==$value ){
            $query->where('brand_name','like','%'.$value.'%');
        }
    }
    public function searchIfShowAttr($query,$value,$data){
        if ("2"!==$value){
            $query->where('if_show','=',$value);
        }
    }
    public function searchBrandLogoAttr($query,$value,$data){
        if (is_array($value) &&  1 == count($value) && 1 == $value[0]){
            $query->where('brand_logo','not null')->where('brand_logo','<>','');
        }
        if (is_array($value) &&  1 == count($value) && 0 == $value[0]){
            $query->where(function ($q){
                $q->where('brand_logo',null)->whereOr('brand_logo','');
        });
     }
    }

    //获取器

    public function getIfshowznAttr($value,$data){
        $ret = [0 => '否',1=>'是'];
        return $ret[$data['if_show']];

    }

    public static function getBrand($data=[],$query=[],$page=false)
    {
        $model=self::field("brand_id,brand_name,tag,brand_logo,sort_order,if_show")
->order('sort_order')->withSearch(array_keys($data),$data);


        $list_rows = Config::has('view.list_rows')?Config::get('view.list_rows'):15;

        $urlPage=Request::get('page',1);
        $lastPage=$model->paginate(['list_rows'=>$list_rows,'query'=>$query])->toArray()['last_page'];
        if($urlPage > $lastPage){
            $urlPage = $lastPage;
        }

        if($page){
            $result=$model->select();
        }else{
            $result=$model->paginate([
                'list_rows' =>$list_rows,
                'fragment'=>'top',
                'query'=>$query,
                'page'=>$urlPage
            ]);
        }
        return $result;
    }

    public function add(object $obj,$data, array $allowField = [], bool $replace = false) {

        try{
            if(empty($data)){
                $obj->error = '新增数据不能为空！';
                return ;
            }
            // 限制写入的字段列表
            if (!empty($allowField))    $obj->allowField($allowField);

            $obj->replace($replace)->save($data);

            return true;
        }catch (\Exception $e){
            $obj->error = $e->getMessage();
            return false;
        }
    }

    public function getInsertId(){
        $data = $this->toArray();
        return $data[$this->pk];
    }

    public function edit($data, $where = [], $allowField = []){

        try{
            if(empty($data)){
                $this->error = "修改数据不能为空!";
                return ;
            }

            if (!empty($allowField)) {
                $this->allowField($allowField);
            }

            if (!empty($where)) {
                $this->setUpdateWhere($where);
                if(isset($data[$this->pk]))     unset($data[$this->pk]);
            }

            return $this->exists(true)->save($data);

        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }

//$db= Db::table("ecm_brand")
//->field("brand_id,brand_name,tag,brand_logo,sort_order,if_show")
//->order('sort_order');
//if(""!==$brand_name){
//$db->where('brand_name','like','%'.$brand_name.'%');
//}
//View::assign('brand_name',$brand_name);
//
//if("2"!==$if_show){
//    $db->where('if_show','=',$if_show);
//}
//View::assign('if_show',$if_show);
//
//if(is_array($brand_logo) &&  1 == count($brand_logo) && 1 == $brand_logo[0]){//有图
//    $db->where('brand_logo','not null')->where('brand_logo','<>','');
//}
//if(is_array($brand_logo) &&  1 == count($brand_logo) && 0 == $brand_logo[0]){//无图
//    $db->where(function ($q){
//        $q->where('brand_logo',null)->whereOr('brand_logo','');
//    });
////
//}
//View::assign('brand_logo',$brand_logo);
//$arr=$db->paginate([
//    'list_rows'=>6,
////                'var_page'=>'p',
////                'page' =>3,
//    'fragment'=>'top',
//    'query'=>input('get.'),
//]);
//$arr->render();
    //
}
